﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;


namespace BITS.Copy
{
    class Program
    {
        [STAThread()]
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                ShowHelp();
                System.Environment.ExitCode = 0;
                return;
            }
            else
            {
                string cmd = args[0];
                cmd = cmd.TrimStart('-', '/').ToLowerInvariant();

                switch (cmd)
                {
                    case "register":
                            CommandLineRegister(args);
                            break;
                    case "unregister":
                            CommandLineUnRegister(args);
                            break;
                    case "complete":
                            CommandLineComplete(args);
                            break;
                    case "copy":
                            CommandLineCopy(args);
                            break;
                    default:
                            ShowHelp();
                            System.Environment.ExitCode = 1;
                            return;
                }
            }
        }

        private static void CommandLineCopy(string[] args)
        {
            if (args.Length > 2)
            {
                string source = args[1];
                string destination = args[2];
                Console.WriteLine(string.Format("Creating copy job from '{0}' to '{1}'", source, destination));
                BITSSambaHelper.Copy(source, destination);
            }
            else
            {
                Console.WriteLine("Invalid parameters.");
                ShowHelp();
                System.Environment.ExitCode = 1;
            }
        }

        private static void CommandLineUnRegister(string[] args)
        {
                ContextMenu.UnRegister();                
        }

        private static void CommandLineRegister(string[] args)
        {
            string directory = string.Empty;
            if (args.Length > 1) directory = args[1];
            if (!string.IsNullOrWhiteSpace(directory) && Directory.Exists(directory))
            {
                Console.WriteLine(string.Format("Register directory '{0}'", directory));
                ContextMenu.Register(directory);
            }
            else
            {
                try
                {
                    Console.WriteLine("Select destination ...");
                    FolderBrowserDialog dlg = new FolderBrowserDialog();
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        directory = dlg.SelectedPath;
                        Console.WriteLine(string.Format("Register directory '{0}'", directory));
                        ContextMenu.Register(directory);
                        return;
                    }
                }
                catch
                {
                }

                Console.WriteLine("Invalid directory.");
                ShowHelp();
                System.Environment.ExitCode = 1;                
            }
        }

        private static void CommandLineComplete(string[] args)
        {
            Guid jobID;
            if (args.Length > 1 && Guid.TryParse(args[1], out jobID))
            {
                Console.WriteLine(string.Format("Complete job '{0}'", jobID.ToString("N")));
                BITSSambaHelper.CompleteJob(jobID);           
            }
            else
            {
                Console.WriteLine("Invalid jobID format");
                ShowHelp();
                System.Environment.ExitCode = 1;                
            }
            
        }



        private static void ShowHelp()
        {
            Console.WriteLine(BITS.Copy.Properties.Settings.Default.Help);
        }

    }
}
